﻿package aga.sokoban;

import aga.sokoban.model.CellKind;
import aga.sokoban.model.Level;
import aga.sokoban.model.LevelCollection;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;

public class BoardView extends View {

	// private ArrayList<PositionInfo> bounds = new ArrayList<PositionInfo>();
	// private HashMap<Tile, BitmapDrawable> faces;
	private Paint textPaint;
	private int screenWidth, screenHeight;
	private BoardController controller;
	private Board board;
	
	private static final double CELL_SIZE = 50.0;
	private int N = 19;
	private LevelCollection lc;

	public BoardView(Context context, AttributeSet attrs) {
		super(context, attrs);
		controller = new BoardController(this);
		lc = LevelProvider.getCollections().get(0);
		setFocusable(true);
		setFocusableInTouchMode(true);
	}

	public Board getBoard() {
		return board;
	}

	public void setBoard(Board value) {
		board = value;
		update();
	}

	public BoardController getController() {
		return controller;
	}

	private void drawTiles(Canvas canvas) {
		Level level = lc.getLevels().get(N);
		
		double xRatio = screenWidth / (CELL_SIZE * level.getWidth());
		double yRatio = screenHeight / (CELL_SIZE * level.getHeight());
		double ratio = Math.min(xRatio, yRatio);
		ratio = Math.min(1, ratio);
		double size = CELL_SIZE * ratio;
		for (int x = 0; x < level.getWidth(); x++) {
			for (int y = 0; y < level.getHeight(); y++) {
				Drawable bmp;
				if (level.getCell(x, y) == CellKind.Wall)
					bmp = ResourceProvider.getWall();
				else
					bmp = ResourceProvider.getSpace();
				
				bmp.setBounds(
						(int)(x * size), 
						(int)(y * size), 
						(int)(x * size + size), 
						(int)(y * size + size));
				bmp.draw(canvas);
			}
		}
	}

	private void drawStatus(Canvas canvas) {
		// String str = String.format("Tiles: %1$s   Pairs: %2$s",
		// board.getTilesCount(), board.getPayersCount());
		// canvas.drawText(str, 5, screenHeight - StatusBarHeight, textPaint);
	}

	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		screenWidth = w;
		screenHeight = h;
		super.onSizeChanged(w, h, oldw, oldh);
	}

	@Override
	public void onDraw(Canvas canvas) {
		canvas.drawColor(Color.GREEN);
		if (screenHeight <= 0 || screenWidth <= 0)
			return;

		drawTiles(canvas);
		drawStatus(canvas);
	}

	// @Override
	// public boolean onTouchEvent(MotionEvent event) {
	// switch (event.getAction()) {
	// case MotionEvent.ACTION_DOWN:
	// onMouseDown((int) event.getX(), (int) event.getY());
	// }
	// return super.onTouchEvent(event);
	// }

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent msg) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			N++;
			if (N >= lc.getLevels().size())
				N = 0;
			this.invalidate();
			controller.undo();
			return true;
		}
		return super.onKeyDown(keyCode, msg);
	}

	// protected void onMouseDown(int x, int y) {
	// for (int i = bounds.size() - 1; i >= 0; i--) {
	// if (bounds.get(i).getBounds().contains(x, y)) {
	// controller.clickTile(bounds.get(i).getPosition());
	// break;
	// }
	// }
	// }

	public void update() {
		invalidate();
	}

	public void showDialog(String title, String message) {
		new AlertDialog.Builder(getContext()).setTitle(title)
				.setMessage(message).setPositiveButton("OK", null).show();
	}
}